package com.dianban.plugin.weixinpay;

import com.dianban.utils.MD5Utils;
import com.dianban.utils.http.HttpClient;
import com.dianban.utils.http.HttpMethod;
import com.dianban.utils.http.HttpParamers;
import com.dianban.weixin.util.WeiXinUtils;

import java.net.URLEncoder;
import java.util.*;

public class LeshuaTestMain {
    public static String appSecret = "D5E8A701B63D495086DCFC4A8B253AE3";

    public static void main(String[] args) throws Exception {
        //get_wxpayface_authinfo();
        //lepos_pay_gateway("h1NkeiRdMGYOGrttvY54ih/YxQvcCdfoT493M6IsP/EToERntI5GCMujZ9v1SQ82fdswujmg4rxM5ZZUVZO4X5iib82fniQmbv8p9A2afctdjeB2R8DgoeYJcf2gpf2tWVcsO2dbK6OzRgkuvpvsxT3uW1IihrLzBNxwxcwl3q0cDTMoPtXN+JI5wMnxNAi4NYURe36CT068hIvyTQwrPEfok3dEnCLkDN24ykN3XRBKNqk43vXc1rsz8v+5rNhjgTBcARAP13MntZ2gJm6SnuvWudnkV6rUk78/PL0GB6MFdr9Zr5BLXwrPvRIb6JONkACtOdp43kWzOuGV6oY2lU1YcFITo4jx5Tk86XPTrbEcvdUr2+4kFolgsbQoBmIZ51lBAKCG7jM/yXzEpFiQqvOD4krurPyIcwpuoeLTfbaambuRI8iWWIJ5FfHn5YW93g6kzeuT+tzrDa+oRcG1MGyg7AxeRGETay2Jl/bzIgEzXkNrSMigO6RZEAhV6PY2Got7It8ubPaA2uPL0hDTsxTv1GkbT5qiRR0hdg+6il647DI82HG4LchKtJDbBFnuJHrKq+pqGustDNfVt2WxcyWwUi80iuXlrXxNKRxQD+QpJka8b9SNIGDtlLkU1wBW+395vSvG4oYidH0UvObSBWzxRG3ZtZ2hMESJwUNu3r5sZCbrCZopK5kBADfAPcpdflBjUTwga/7kb6PpiCAZpldJ4uAAUb60pwH0NhlgyQwmNG3jiuY97NhD4NkPfVwkXySbNqE0WkYzz2UrktYM3CuK/VchY62b0jx4tnLu8YbyF63c1m0WdQ9czG7jQixYJ5Epgr+JNl3/9Za7YQJKE28bFCjUxfe9UILLVh03ujujHnMNlVElXt4/KCqV3Ft/o4RN/kIBUxiOP3vweQ==");
        lepos_pay_gateway("144824802075108486");
    }

    public static void lepos_pay_gateway(String authCode) throws Exception {
        String sn = UUID.randomUUID().toString().replaceAll("-", "");
        String createNoncestr = WeiXinUtils.CreateNoncestr();
        HashMap<String, String> params = new HashMap<>();
        params.put("service", "upload_authcode");
        params.put("auth_code", authCode);
        params.put("merchant_id", "9960219801");
        params.put("third_order_id", sn);
        params.put("amount", "1");
        //params.put("notify_url", URLEncoder.encode(getNotifyUrl(sn)));
        params.put("nonce_str", createNoncestr);
        String sign = getSign(params);
        System.out.println(sign);
        params.put("sign", sign); // 参数签名
        String bizQueryParams = FormatBizQueryParaMap(params, false);
        System.out.println(bizQueryParams);
        String url = "https://paygate.leshuazf.com/cgi-bin/lepos_pay_gateway.cgi" + "?" + bizQueryParams;
        System.out.println(url);
        HttpParamers httpParamers = new HttpParamers(HttpMethod.POST);
        String resultService = HttpClient.doService(url, httpParamers, null);
        System.out.println(resultService);
    }

    public static String getSiteUrl() {
        //return "http://yun.dianbang-st.com:9006/api/nengliang/wechat/asyncNotify/";
        return "https://rongyaotupay.aokxm.com/api/nengliang/wechat/asyncNotify/";
    }

    public static String getNotifyUrl(String sn) {
        return getSiteUrl()+sn;
    }

    public static void get_wxpayface_authinfo() throws Exception {
        HashMap<String, String> params = new HashMap<>();
        //params.put("sub_appid", "wx96e5ce0781e0eee9");
        //params.put("sub_mch_id", "1613178618");
        params.put("sub_mch_id", "479779456");
        params.put("merchant_id", "9960219801");
        params.put("raw_data", "gko11VpIN9BkIptV3rAqbNCiOPZlQIH8W0i1KCrnQjwLmffFt4m9yhQOK67ndNpZnHvpI4jCD5la3UsTTOx35W+x+jE9DrSlnMdc82V2ctwrgS/QDf3SLXnCOoBRjiFxooXC9pVWnF120U3lDmYs6bFEAKTtfGxfmqEEFRYn587uhreOdplIpvHQkn5UAeEUmnImfSdqvbjLeRCpTdUe2a24BotjVcQvVwMnFIXjEBoIr2j21YIRvYbxa7SsZEpnry/VgNzGlWj1cKrJbFZMa+FfLR4Sd9M4gzFW73yl9VJb0MT0+AG+Qw8BfimXEF4ghfKj33UoBrI0ELtuBsRo+/XZ8KdulbgDkDjHd3tLb2uGXSizU+OV8DYo2ZLKOUesALf/XwZ8m/S7rSh90YLzoNXR/xkjQm3SA/ukmHS0uxKZ3PfUfv6EJNrv/W5dTwhEPtl1dOVq1VMR3OKcglqfNmQQ1hcCtgjIlFBVkdCd3Jm8zhG852iyCVUC5BDSntcjy4od3wAHl+yKIXzUU+TJBQlcgUZu9mReoheHn2hYUmx9NZ7RLgaO+w2XJ/vkwq7T/5y7/HlfV0c7Xwhv2TmnBHB1HC/GhEzfMo0C/oIXkMQ6oGhj2eJpHojV9sm5eR/QR2kG6ad2gut78Y+vt1w9HKuL5Zgp+oeMyh42dTPnkuhCk1mqgRx5p4gXpevnFB51kylN7jFb1TVE0/wvJ7kFa8kYW/XJzExEg63gZXfgBbGiiZnYGxFZ0mzazE0czlfhma4LIvUMFwsFnQ==");
        params.put("store_id", "13aa");
        params.put("store_name", "厦门软件园门店");
        params.put("device_id", "13bb");
        String sign = getSign(params);
        System.out.println(sign);
        params.put("sign", sign); // 参数签名
        String bizQueryParams = FormatBizQueryParaMap(params, true);
        System.out.println(bizQueryParams);
        //String url = "https://paygate.leshuazf.com/open_api/get_wxpayface_authinfo.cgi" + "?" + bizQueryParams;
        String url = "https://paymeans.leshuazf.com/open_api/get_wxpayface_authinfo.cgi" + "?" + bizQueryParams;
        //String url = "https://paymeans.leshuazf.com/open_api/get_wxpayface_authinfo.cgi";
        System.out.println(url);
        HttpParamers httpParamers = new HttpParamers(HttpMethod.POST);
//        httpParamers.addParam("sub_mch_id", "1613178618");
//        httpParamers.addParam("merchant_id", "9960219801");
//        httpParamers.addParam("raw_data", "hzPmXkQ2qnrW41nqyMhnTtrCg+yCxGBkmoy0vNVwDwG3a7bLRHGx5uQRqEDQyzXfz8zmhjGFINXXkWIwg46C1Dxc3jH9SA8YU0nFiY3H5OuhlDxLLzfNcWkZh7vHeU8GWzJNuFs1WaezCFGy/G2ATG8TBsAlJIV3/c6uZlkxR96qzXTat00S/LplUySqLbVk2i98IfSLZIqzE30lg8v992DRP5waQfkA6mBSnxUvGbs9y/Xk/oK4tvIynkBO0jwkb3ghPy95W96PbBDmpOo2zRkCg6xzXIA4uXqONw7MVzYvBRIiwtMnk1osYvsDD5u1gesH16ZAN2qg0oCzqlRCKuaXVnjagbHNqkdwc7oqZx2S3vjSTv7CiYaRK7fu3PmquRDlo0SHIaQCh4Sy+ncWJF0euAD7e45G9xoAbF1AGehJ8Dj9JrEHz04Od5Rx1TnxF2RvyEsAFaX/y31dI9Fquae80pQXfotJWtwoQVsTA25V1HPq3E/yGz9jtlI1nx7sqZA4dSKWD5Jb7yrZArKjhpjodkoUiLENmGV2rNvfMrY5w5gTbq2mB0i84orxAkM4TT9gdp2Dcskr5DiFDuB+ZxE7FflnUuP4/NeW5nYJpua7vLmTP40Qm31aCUktPKUKy049vTnNlkh+ZtXIfzDQMPiH1itvF964Eld2uPXeSZAjZm75ntOFzINfJ0iUlIBZcZsXu7LSU+eRqQlzBiUy1OrzbjAia0SM77HZQC4g8asVoekhqJWzsqyX27+Rg+GJLwxEY0N4bX6QQFY=");
//        httpParamers.addParam("store_id", "13aa");
//        httpParamers.addParam("store_name", "厦门软件园门店");
//        httpParamers.addParam("device_id", "13bb");
//        httpParamers.setJsonParamer();
        String resultService = HttpClient.doService(url, httpParamers, null);
        System.out.println(resultService);
    }

    public static String getSign(HashMap<String, String> params) throws Exception {
        // 签名步骤一：按字典序排序参数
        String str = FormatBizQueryParaMap(params, false);
        // 签名步骤二：在string后加入KEY
        str += "&key=" + appSecret;
        // 签名步骤三：MD5加密 签名步骤四：所有字符转为大写
        str = MD5Utils.getMD5Str(str).toUpperCase();
        return str;
    }

    public static String FormatBizQueryParaMap(Map<String, String> paraMap, boolean urlencode) throws Exception {

        String buff = "";
        List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(paraMap.entrySet());

        Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                return (o1.getKey()).toString().compareTo(o2.getKey());
            }
        });
        for (int i = 0; i < infoIds.size(); i++) {
            Map.Entry<String, String> item = infoIds.get(i);
            String key = item.getKey();
            String val = item.getValue().toString();
            // 组装需要传sign过去，不然验签有问题
            if (null != val && !"".equals(val) && !"sign1".equals(key) && !"key".equals(key)) {
                if (urlencode) {
                    val = URLEncoder.encode(val, "utf-8");
                }
                buff += key + "=" + val + "&";

            }
        }

        if (buff.isEmpty() == false) {
            buff = buff.substring(0, buff.length() - 1);
        }
        return buff;
    }
}
